import 'package:flutter_keep_book/core/extensions/list.dart';
import 'package:flutter_keep_book/core/extensions/map.dart';

import '../../../core/network/network.dart';

class ItemTypeList {
  ItemTypeList();

  List<ItemTypeVo>? list;

  static ItemTypeList? fromJson(dynamic json) {
    if (json is! Map || json.isEmpty) {
      return null;
    }
    return ItemTypeList()..list = ItemTypeVo.listFromJson(json['list']);
  }

  /// 请求分类列表
  static Future<Result<ItemTypeList>> requestItemTypeList({
    bool withLoading = false,
    int page = 1,
  })  =>
    Api.requestPost(
      path: HttpUrl.itemTypeList,
      withLoading: withLoading,
      param: {'page': page},
    ).then(
    (value) => value.map(
    (data) => ItemTypeList.fromJson(data),
    ),
    );
}

class ItemTypeVo {
  int? id;
  int? itemParentId;
  int? itemLevel;
  String? itemName;

  ItemTypeVo();

  static List<ItemTypeVo>? listFromJson(dynamic json) {
    if (json is! List) return null;
    return json.compactMap((e) => fromJsonNullable(e));
  }

  static ItemTypeVo? fromJsonNullable(dynamic json) {
    if (json is! Map || json.isEmpty) return null;
    return ItemTypeVo.fromJson(json);
  }

  ItemTypeVo.fromJson(Map json)
      : id = json.intValue('id') ?? 0,
        itemParentId = json.intValue('itemParentId') ?? 0,
        itemLevel = json.intValue('itemLevel') ?? 0,
        itemName = json.stringValue('itemName') ?? "";
}
